/*
 * The Open Source Unix Installer
 *
 * The MIT License
 *
 * Copyright 2011-2012 Andrey Pudov.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/*
 * @author  Andrey Pudov    <andrey@andreypudov.com>
 * @version 0.00.00
 * @name    u_main.h
 * @date:   Feb 10, 2012
 */

#ifndef INSTALLER_UTIL_U_MAIN_H_
#define INSTALLER_UTIL_U_MAIN_H_

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include <wchar.h>

#ifdef __cplusplus
extern "C" {
#endif

void u_installer_main_print(const char *unit, const char *function, 
                            const bool result);
void u_installer_main_trace(const char *unit, const char *function,
                            const char *format, ...);

/* public code definition */
void u_installer_main_print(const char *unit, const char *function, 
                            const bool result) {
    const char FUNCTION[] = "\033[0;34m";  /* blue color ASCII sequence */
    const char NORMAL[]   = "\033[0;37m";  /* gray color ASCII sequence */
    const char UNIT[]     = "\033[0;33m";  /* orange color ASCII sequence */
    const char WARNING[]  = "\033[0;31m";  /* red color ASCII sequence */
    
    printf("Unit: %s%16s%s, function: %s%-20s%s, result: %6s\n", 
           UNIT, 
               unit,
               NORMAL,
           (result) ? FUNCTION : WARNING,
               function,
               NORMAL,
           (result) ? "\033[0;32mpassed\033[0;37m" : "\033[0;31mfailed\033[0;37m");

    if (errno != 0) {
        fprintf(stdout, "[ERROR|%s] %s\n",
                "u_installer_main_print", 
                "\033[0;31mUNIT TEST RETURNED NONZEO EXIT CODE\033[0;37m");
        fprintf(stdout, "[ERROR|%s|%s] %s\n%s\n",
                unit, function, strerror(errno), 
                "\033[0;31mTEST BUILD FAILED\033[0;37m");

        exit(EXIT_FAILURE);
    }
}

void u_installer_main_trace(const char *unit, const char *function,
                            const char *format, ...) {
    const char NORMAL[]   = "\033[0;37m";  /* gray color ASCII sequence */
    const char UNIT[]     = "\033[0;35m";  /* orange color ASCII sequence */
    
    va_list argument_list;
    
    va_start(argument_list, format);
    
    printf("%sinstaller_%s_%s%s stack trace:\n", UNIT, unit, function, NORMAL);
    vprintf(format, argument_list);
    
    va_end(argument_list);
}

#ifdef __cplusplus
}
#endif

#endif /* INSTALLER_UTIL_U_MAIN_H_ */ 
